<?php
namespace action\forum;
use \cass\Cache;

class indexAction extends \cass\Action {

    /**
     * Méthode de cache
     * @return Integer Type de cache (Cache::<type>)
     */
    public function cache() {
        return array(
            'execute' => Cache::SESSION_MULTIPLE
        );
    }

    /**
     * Méthode de l'index
     */
    public function execute() {
        $this->setTitle('Forums');

        $this->categories = $this->doctrine->createQuery('SELECT c FROM ForumsCategory c ORDER BY c.pos ASC')->execute();
    }

    /**
     * Marquer tous les messages comme lus
     */
    public function executeRead() {
        $categories = $this->doctrine->getRepository('ForumsCategory')->findAll();
        foreach ($categories AS $category) {
            if (!$category->hasForumCredential($this->getSession()))
                continue;

            foreach ($category->getForums() AS $key => $forum) {
                if (!$forum->hasCredential($this->getSession()))
                    continue;

                foreach ($forum->getDiscussions() AS $discussion) {
                    $discussion->read($this->getSession());
                    $this->doctrine->persist($discussion);
                }
            }
        }
        $this->doctrine->flush();
        $this->redirect('forum');
    }

}